package com.css.zfzx.fwsjcj.modules.expert.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fwsjcj.modules.expert.entity.FwExpertEntity;
import com.css.zfzx.fwsjcj.modules.expert.entity.FwUserDivisionEntity;
import com.css.zfzx.fwsjcj.modules.expert.service.FwExpertService;
import com.css.zfzx.fwsjcj.modules.expert.vo.ExpertVo;
import com.css.zfzx.fwsjcj.modules.expert.vo.FwUserDivisionVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2021/12/21 9:16
 **/
@RestController
@RequestMapping("/fw/expert")
@Slf4j
public class FwExpertController {
    /**
     * @Description : 分页查询
     * @Return : 
     * @Date : 2022/3/10 9:43
     */
    @Autowired
    FwExpertService fwExpertService;
    @GetMapping("page")
    public RestResponse getPage(HttpServletRequest request, ExpertVo expertVo) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            json = fwExpertService.getPage(request, curPage, pageSize, expertVo);
            json.put("message", "查询成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /**
      * @Description : 保存
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @PostMapping("save")
    public RestResponse save(@RequestBody ExpertVo expertVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            fwExpertService.save(expertVo);
            jsonObject.put("message", "分配成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "分配失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
      * @Description : 编辑
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @PostMapping("edit")
    public RestResponse edit(@RequestBody ExpertVo expertVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            fwExpertService.edit(expertVo);
            jsonObject.put("message", "编辑成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "编辑失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
      * @Description : 初始化树
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @GetMapping("initZtree")
    public RestResponse initZtree() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<DivisionEntity> divisionEntityList = fwExpertService.initZtree();
            restResponse = RestResponse.succeed(divisionEntityList);
        } catch (Exception e) {
            log.error("查询行政区划失败!", e);
            restResponse = RestResponse.fail("查询行政区划失败");
        }
        return restResponse;
    }

    /**
     * 专家角色的业务方向查询
     *
     * @return
     */
    @GetMapping("getExpertStructureType")
    public RestResponse getExpertStructureType() {
        RestResponse restResponse = null;
        try {
            JSONObject jsonObject=new JSONObject();
            String userId = PlatformSessionUtils.getUserId();
            String StructureType=fwExpertService.getExpertStructureType(userId);
            jsonObject.put("statusList", StructureType);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
      * @Description : 查询专家数据查看范围中的省
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @GetMapping("getProvince")
    public RestResponse getAllProvince() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = PlatformSessionUtils.getUserId();
            List<Map<String,Object>> provinceList = fwExpertService.getProvince(userId);
            restResponse = RestResponse.succeed(provinceList);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
      * @Description : 查询专家数据查看范围中的市
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @GetMapping("getCity")
    public RestResponse getCity() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = PlatformSessionUtils.getUserId();
            List<Map<String,Object>> cityList = fwExpertService.getCity(userId);
            restResponse = RestResponse.succeed(cityList);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
      * @Description : 查询专家数据查看范围中的省下对应的所有市
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @GetMapping("/getProvincialCity/{provinceId}")
    public RestResponse getProvincialCity(@PathVariable String provinceId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = PlatformSessionUtils.getUserId();
            List<Map<String,Object>> cityList = fwExpertService.getProvincialCity(userId,provinceId);
            restResponse = RestResponse.succeed(cityList);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取专家数据类型
     *
     * @return
     */
    @GetMapping("getExpertDataType")
    public RestResponse getExpertDataType() {
        RestResponse restResponse = null;
        try {
            JSONObject jsonObject=new JSONObject();
            String userId = PlatformSessionUtils.getUserId();
            String dataType=fwExpertService.getExpertDataType(userId);
            jsonObject.put("dataType", dataType);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
      * @Description : 查询专家数据查看范围
      * @Return : 
      * @Date : 2022/3/10 9:43
      */
    @GetMapping("/getDataRange/{userId}")
    public RestResponse getDataRange(@PathVariable String userId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<FwUserDivisionEntity> dataRange = fwExpertService.getDataRange(userId);
            restResponse = RestResponse.succeed(dataRange);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    @GetMapping("/getProvincialCityName")
    public RestResponse getProvincialCityName() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = PlatformSessionUtils.getUserId();
            Map<String,Object> dataRange = fwExpertService.getProvincialCityName(userId);
            restResponse = RestResponse.succeed(dataRange);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

}
